package com.oyesgo.statistics.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.oyesgo.statistics.dao.LogInfoDao;
import com.oyesgo.statistics.log.LogUtils;
import com.oyesgo.statistics.model.LogInfo;
import com.oyesgo.statistics.utils.GlobalContent;
import com.oyesgo.statistics.utils.JdbcUtil;

/**
 * @FileName LogInfoDaoImpl.java
 * @Description: 日志操作DAO实现类
 * @author rose
 * @version 1.0
 * <p>Company: OYESGO</p>
 * <p>Copyright: Copyright (c) 2010</p>
 */
public class LogInfoJdbcDaoImpl implements LogInfoDao {
	public void insertList(List<LogInfo> logInfoList) {
		if (logInfoList == null || logInfoList.size() == 0) {
			return;
		}
		Connection conn = JdbcUtil.getConnectionByDbcp();//从池中获取连接连接
//		Connection conn = JdbcUtil.getConnection();//直接取连接连接
		try {
			conn.setAutoCommit(false);//设定事务自动处理为false
		} catch (SQLException e1) {
			GlobalContent.flag = true;
			e1.printStackTrace();
		}
		//开始拼凑字符串 固定代码 start
		StringBuffer sqlSb = new StringBuffer();
		sqlSb.append("insert into loginfo(");
		sqlSb.append("userip,visittime,visiturl,visitstate,useragent,fromurl,refkey,cookie,channeltype,pagetype");
		sqlSb.append(") ");
		sqlSb.append(" values(?,?,?,?,?,?,?,?,?,?)");
		//开始拼凑字符串 固定代码 end
		String sql = sqlSb.toString();
		LogUtils.printlnToFile(sql);
		/////////开始执行插入///////////////
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			LogInfo logInfo = null;
			//取出list内对象挨个插入
			for (Iterator<LogInfo> iterator = logInfoList.iterator(); iterator.hasNext();) {
				logInfo = iterator.next();
				ps.setString(1,logInfo.getUserIp());
				ps.setObject(2,logInfo.getVisitTime());
				ps.setString(3,logInfo.getVisitUrl());
				ps.setInt(4,logInfo.getVisitState());
				ps.setString(5,logInfo.getUserAgent());
				ps.setString(6,logInfo.getFromUrl());
				ps.setString(7,logInfo.getRefKey());
				ps.setString(8,logInfo.getCookie());
				ps.setString(9,logInfo.getChannelType());
				ps.setInt(10,logInfo.getPageType());
				ps.execute();
				logInfo = null;//清理一下
			}
			conn.commit();//提交事务
		} catch (SQLException e) {
			try {
				conn.rollback();//回滚事务
				GlobalContent.flag = true;
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public List<LogInfo> getAllList() {
		Connection conn = JdbcUtil.getConnectionByDbcp();//从池中获取连接连接
		//开始拼凑字符串 固定代码 start
		StringBuffer sqlSb = new StringBuffer();
		sqlSb.append("select * from loginfo");
		//开始拼凑字符串 固定代码 end
		String sql = sqlSb.toString();
		System.out.println(sql);
		List<LogInfo> resultList = new ArrayList<LogInfo>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			LogInfo logInfo = null;
			while(rs.next()){
				logInfo = new LogInfo();
				logInfo.setId(rs.getInt("id"));
				logInfo.setCookie(rs.getString("cookie"));
				resultList.add(logInfo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs, ps, conn);
		}
		System.out.println(resultList.get(0).getCookie());
		return null;
	}
}
